package com.voja.salonschedule.customers;

import java.util.ArrayList;
import java.util.Locale;

import com.voja.salonschedule.Customers;
import com.voja.salonschedule.Index;
import com.voja.salonschedule.R;
import com.voja.salonschedule.data.Customer.CustomerItem;
import com.voja.salonschedule.data.CustomerBalance.Balance;

import android.app.Activity;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;

public class Adapter extends ArrayAdapter<CustomerItem> {

    private Context context;
    private int resourceId;
    protected ArrayList<CustomerItem> customers;
    protected ArrayList<CustomerItem> customersFiltered;
    public String keyword = "";
    
    @SuppressWarnings("unchecked")
    public Adapter(Context context, int resourceId, ArrayList<CustomerItem> customers, String keyword) {
        super(context, resourceId, customers);
        
        this.context = context;
        this.resourceId = resourceId;
        this.customers  = (ArrayList<CustomerItem>)customers.clone();
        this.customersFiltered = customers;
        this.keyword = keyword;
    }
    
    @Override
    public CustomerItem getItem(int pos) {
        return customersFiltered.get(pos);
    }
    
    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        CustomerItem item = getItem(pos);
        
        if (convertView == null) {
            convertView = ((Activity)context).getLayoutInflater()
                                             .inflate(resourceId, parent, false);
        }
        
        // call button
        initCallButton(convertView, item);
        
        // message button
        initMessageButton(convertView, item);
        
        // delete button
        initDeleteButton(convertView, item);
        
        populate(convertView, item);
        
        return convertView;
    }
    
    private void initCallButton(View view, CustomerItem customer) {
        ImageView imgCall = (ImageView)view.findViewById(R.id.imgCall);
        if (imgCall != null) {
            if (customer.hasPhone()) {
                imgCall.setContentDescription(customer.phone);
                imgCall.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String phone = ((ImageView)v).getContentDescription().toString();
                        Index.makeCall(context, phone);
                    }
                });
            } else {
                imgCall.setVisibility(View.GONE);
            }
        }
    }
    
    private void initMessageButton(View view, CustomerItem customer) {
    	ImageView imgMessage = (ImageView)view.findViewById(R.id.imgMessage);
    	if (imgMessage != null) {
    		if (customer.hasPhone()) {
    			imgMessage.setContentDescription(customer.phone);
                imgMessage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String phone = ((ImageView)v).getContentDescription().toString();
                        Index.sendMessage(context, phone);
                    }
                });
    		} else {
    			imgMessage.setVisibility(View.GONE);
    		}
    	}
    }
    
    private void initDeleteButton(View view, CustomerItem customer) {
        ImageView imgDelete = (ImageView)view.findViewById(R.id.imgDelete);
        if (imgDelete != null) {
            imgDelete.setContentDescription(customer.name);
            if (imgDelete != null) {
                imgDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String customerName = v.getContentDescription().toString();
                        ((Customers)context).deleteCustomer(customerName, false);
                    }
                });
            }
        }
    }
    
    private void populate(View view, CustomerItem customer) {
        TextView  name = (TextView)view.findViewById(R.id.txtCustomerName);
        ImageView img  = (ImageView)view.findViewById(R.id.imgCustomer);
        
        highlight(name, customer.name);
        img.setImageBitmap(customer.bitmap(R.drawable.contact));
        
        ImageView imgBalance = (ImageView)view.findViewById(R.id.imgBalance);
        if (imgBalance != null) {
	        Balance balance = customer.getBalance();
	        if (balance.isPositive()) {
	        	if (balance.value() == 0) {
	        		imgBalance.setVisibility(View.GONE);
	        	} else {
	        		imgBalance.setImageResource(R.drawable.balance_ok);
	        	}
	        } else {
	        	imgBalance.setImageResource(R.drawable.balance_error);
	        }
        }
    }
    
    @Override
    public Filter getFilter() {
        return new ListFilter(this, false);
    }
    
    public void highlight(TextView tv, String name) {
        android.text.Spannable span = new SpannableString(name);
        
        if (keyword == null) {
            keyword = "";
        }
        
        if(keyword.length()>0) {	       
	        int start = name.toLowerCase(Locale.getDefault()).indexOf(keyword.toLowerCase(Locale.getDefault()));
	        int end   = start + keyword.length();
	        Activity a = (Activity)context;
	        int bgHighlight = a.getResources().getColor(R.color.highlightBackground);
	        int fgHighlight = a.getResources().getColor(R.color.highlightForeground);
	       
	        if (start >= 0){
	            span.setSpan(new BackgroundColorSpan(bgHighlight), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
	            span.setSpan(new ForegroundColorSpan(fgHighlight), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
	            tv.setText(span);
	        }
        } else {
        	tv.setText(name);
        }
    }

}
